package com.zhenlong01.zhentest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhenlong01.zhentest.entity.Menu;
import com.zhenlong01.zhentest.entity.MenuButton;
import com.zhenlong01.zhentest.util.AccessTokenUtil;
import com.zhenlong01.zhentest.util.NetUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019-08-26 19:12
 */
@RestController
public class MenuController {
    @Resource
    RedisTemplate<String,Object> redisTemplate;

    List<MenuButton> buttons = new ArrayList<MenuButton>();
    String menu_url = " https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";

    @GetMapping("/find_menu")
    public Menu find_menu(){
        Menu m = (Menu) redisTemplate.opsForValue().get("wechatMenu");
        return m;
    }

    @PostMapping("/update_menu")
    public JSONObject update_menu(@RequestBody Menu menu){


        menu.getButton().get(0).
                setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AccessTokenUtil.APPID+"&redirect_uri=http://47.110.59.63/index.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");

        menu.getButton().get(2).
                setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AccessTokenUtil.APPID+"&redirect_uri=http://47.110.59.63/repair_man.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");

        System.out.println(JSON.toJSONString(menu));
        JSONObject obj = NetUtil.post(menu_url+
                        redisTemplate.opsForValue().get("access_token"),
                JSON.toJSONString(menu));

        redisTemplate.opsForValue().set("wechatMenu",menu);

        return obj;
    }

}
